package com.dayaokeji.rhythmschoolstudent.client.common.exam.adapter;

import android.support.v7.widget.AppCompatCheckBox;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.dayaokeji.rhythmschoolstudent.R;
import com.dayaokeji.rhythmschoolstudent.client.common.ShowBigImageActivity;
import com.dayaokeji.rhythmschoolstudent.client.common.exam.subject_type.utils.SubjectTypeUtil;
import com.dayaokeji.server_api.domain.ExamQuestion;
import com.dayaokeji.server_api.domain.SubjectAnswer;

import java.util.List;

/**
 * Created by Gorden on 2018/5/5.
 */
public class SubjectChooseItemAdapter extends BaseQuickAdapter<ExamQuestion.ChoiceInfo,BaseViewHolder> {

    private SubjectAnswer subjectAnswer;
    private int subjectType;
    private boolean isExamResult;
    private boolean isShowResult;

    public SubjectChooseItemAdapter(SubjectAnswer subjectAnswer,int subjectType,boolean examResult,boolean isShowResult) {
        super(R.layout.item_subject_item, null);
        this.subjectAnswer = subjectAnswer;
        this.subjectType = subjectType;
        this.isExamResult = examResult;
        this.isShowResult = isShowResult;
    }

    public SubjectChooseItemAdapter() {
        super(R.layout.item_subject_item, null);
    }

    @Override
    protected void convert(BaseViewHolder helper, ExamQuestion.ChoiceInfo item) {
        helper.addOnClickListener(R.id.cb_choose_subject_item);
        helper.setText(R.id.tv_item_index,String.format("%s .",item.getIndex()));
        helper.setText(R.id.tv_item_content,item.getContent());

        final AppCompatCheckBox checkBox = (AppCompatCheckBox) helper.getView(R.id.cb_choose_subject_item);
        if (isShowResult) {
            checkBox.setButtonDrawable(R.drawable.result_checked_style);
        }else{
            checkBox.setButtonDrawable(R.drawable.checked_style);
        }
        if (isExamResult) {
            checkBox.setEnabled(false);
        }else{
            checkBox.setEnabled(true);
        }
        if (subjectAnswer != null && !TextUtils.isEmpty(item.getIndex())) {
            if (subjectType == SubjectTypeUtil.SINGLE_CHOOSE) {
                checkBox.setChecked(item.getIndex().equals(subjectAnswer.getAnswer()));
            }else{
                checkBox.setChecked(subjectAnswer.getChooseIndex().contains(item.getIndex()));
            }
        }

        final RecyclerView recyclerView = (RecyclerView) helper.getView(R.id.rv_subject_item_img);
        setupRecyclerView(recyclerView,item.getResourceList());
    }

    private void setupRecyclerView(RecyclerView recyclerView,List<Integer> images) {
        recyclerView.setLayoutManager(new GridLayoutManager(mContext,4));
        final SubjectImgAdapter subjectImgAdapter = new SubjectImgAdapter(images);
        subjectImgAdapter.setOnItemClickListener(subjectItemImgListener);
        recyclerView.setAdapter(subjectImgAdapter);
        recyclerView.setTag(subjectImgAdapter);

    }

    private BaseQuickAdapter.OnItemClickListener subjectItemImgListener = new OnItemClickListener() {
        @Override
        public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
            final Integer imgId = (Integer) adapter.getData().get(position);
            ShowBigImageActivity.navigation(mContext,imgId);
        }
    };



}
